﻿using System;
using System.Collections.Generic;
using NHibernate;
using ParaEditor.Common;
using ParaEditor.Domain.Entities;

namespace ParaEditor.DAO.ParaDAO
{
    public class QfPercentAllDAO:BaseDAO
    {
        private static QfPercentAll param = new QfPercentAll();

        public QfPercentAllDAO()
            : base(param)
        {
        }

        public ResultValue DelQfPercentAllByInfoVerAndRecordID(int infover)
        {
            ResultValue rValue = new ResultValue();
            try
            {

                string sqlText = "delete from tbl_metro_qf_percent_all where  info_ver = " + infover;
                ISQLQuery query = Session.CreateSQLQuery(sqlText);
                query.ExecuteUpdate();
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = -1;
                rValue.Msg = e.Message;
            }
            finally
            {
                Session.Flush();
                Session.Clear();
            }
            return rValue;
        }

        public void CreateObjectNonFlushSession(QfPercentAll objs)
        {
            try
            {
                //Session.Merge(objs);
                Session.Save(objs);
            }
            catch (Exception e)
            {
                System.Console.Out.WriteLine("---");
            }
        }

        public ResultValue FlushSession()
        {
            ResultValue rValue = new ResultValue();
            try
            {
                Session.Flush();
                Session.Clear();
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                System.Console.Out.WriteLine("---");
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            return rValue;
        }

        public ResultValue CloseSession()
        {
            ResultValue rValue = new ResultValue();
            try
            {
                Session.Close();
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            return rValue;
        }

        public int GetMaxID()
        {
            string sqlText = "select nvl(max(info_ver),0) as maxid from tbl_metro_qf_percent_all";
            ISQLQuery query = Session.CreateSQLQuery(sqlText).AddScalar("maxid", NHibernateUtil.Int32);
            int maxid = Convert.ToInt32(query.UniqueResult());
            return maxid;
        }

        public ResultValue BatchCreateObject(IList<QfPercentAll> objs)
        {
            ResultValue rValue = new ResultValue();
            DelQfPercentAllByInfoVerAndRecordID(objs[0].qfPercentAllPk.infoVer);
            try
            {
                int count = objs.Count;
                for (int i = 1; i < count + 1; i++)
                {
                    Session.Merge(objs[i - 1]);
                    if (i % 200 == 0)
                    {
                        Session.Flush();
                        Session.Clear();
                    }
                }
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            finally
            {
                Session.Flush();
                Session.Clear();
            }
            return rValue;
        }
    }
}
